package edu.cuit.avatar.array;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/4 8:58
 */
public class YanghuiTriangle {
    public static void main(String[] args) {
        int[][] triangles = triangle(6);
        printTriangle(triangles);
    }

    public static int[][] triangle(int line){
        int[][] triangles = new int[line][];
        //给每一行定义元素个数
        for(int i = 0; i < triangles.length; i++) {
            //每一行的列数等于行数
            triangles[i] = new int[i + 1];
        }

        //给每一列初始化数据
        for (int i = 0; i < triangles.length; i++) {
            //每行第一列和最后一列均为1
            triangles[i][0] = triangles[i][i] = 1; //列数 = 行数
            for (int j = 1; j < triangles[i].length - 1; j++) {
                triangles[i][j] = triangles[i - 1][j - 1] + triangles[i - 1][j];
            }
        }
        return triangles;
    }

    public static void printTriangle(int[][] triangles){
        for (int i = 0; i < triangles.length; i++) {
            for (int j = 0; j < triangles[i].length; j++)
                System.out.printf("%3d  ",triangles[i][j]);
            System.out.println();
        }
    }
}
